﻿using LY.Entity;
using LY.Entity.Entities;
using LY.Entity.PageSearch;
using LY.Entity.SearchEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LY.DataAccess.Repository
{
    public  class InformationRepository :BaseRepository
    {
        /// <summary>
        /// 检索U_Information
        /// </summary>
        /// <param name="search"></param>
        /// <param name="view"></param>
        /// <returns></returns>
        public  PagedList<U_Information_EX> QueryInformationList(InformationSearch search,int? touser, PageView view) 
        {
            string cols = @"Id
                        ,Title
                        ,Content
                        ,CreateDate
                        ,FromUser
                        ,ToUser
                        ,Status";
            string sqlWhere = "and ToUser='" + touser + "'";
            if (!string.IsNullOrEmpty(search.Content))
            {
                sqlWhere += " AND (Content LIKE '%" + search.Content + "%')";
            }
            return base.PageGet<U_Information_EX>(view, cols,
                @"U_Information"
                , sqlWhere
                , "CreateDate DESC ",
                "");
        }
        /// <summary>
        /// 根据发送者Id,检索information 列表
        /// </summary>
        /// <param name="memberid"></param>
        /// <returns></returns>
        public List<U_Information_EX> GetInformation(int? touser)
        {
            string sql = @"SELECT information.Id
                          ,[Title]
                          ,[Content]
                          ,[CreateDate]
                          ,[FromUser]
                          ,[ToUser]
                          ,member.Name as ToUserName
                      FROM [U_Information] information
                      Left join U_Member member on information.FromUser=member.ID 
                      where FromUser=0 or ToUser=@ToUser";
            return base.Query<U_Information_EX>(sql, new { ToUser = touser });
        }

        //根据发送者编号检索Informaion列表
        public List<U_Information> GetInformationByFromUser(int? fromuser)
        {
            string sql = @"SELECT [Id]
                          ,[Title]
                          ,[Content]
                          ,[CreateDate]
                          ,[FromUser]
                          ,[ToUser]
                      FROM [SHLY].[dbo].[U_Information]
                where FromUser=@FromUser";
            return base.Query<U_Information>(sql, new { FromUser = fromuser });
        }
        /// <summary>
        /// 根据发送者Id，查询未读消息记录数
        /// </summary>
        /// <param name="FromUser"></param>
        /// <returns></returns>
        public int GetInformationCount(int? touser)
        {
            string sql = @"SELECT Count(*)
                      FROM [U_Information]
                where ToUser=@ToUser and Status=1";
            return base.Get<int>(sql, new { ToUser = touser });
        }

        /// <summary>
        /// 添加一条Information
        /// </summary>
        /// <param name="information"></param>
        /// <returns></returns>
        public int InsertInformation(U_Information information) 
        {
            return (int)base.Insert(information);
        }
        /// <summary>
        /// 根据主建Id 修改消息状态
        /// status   0：已读 , 1:未读
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Modify(int? id) 
        {
            string sql = "update U_Information set Status=0 where Id=@Id";
            return base.Excute(sql, new { Id = id });
        }
        //本人消息列表
        public U_Information_EX GetInformationById(int? id) 
        {
            string sql = @"SELECT information.Id
                                            ,[Title]
                                            ,[Content]
                                            ,[CreateDate]
                                            ,[FromUser]
                                            ,[ToUser]
                                            ,member.Name as FromUserName
                                            ,information.Status
                                        FROM [U_Information]  information
                                      left  outer join  U_Member member on information.FromUser=member.ID
                                      where information.Id='" + id + "'";
            return base.Get<U_Information_EX>(sql,null);
        }
        //删除
        public int Del(int? id) {
            string sql = "delete U_Information where id=@id";
            return base.Excute(sql, new { Id = id });
        }
    }
}
